package com.example.mytestdemo.customviewbook

import android.animation.ObjectAnimator
import android.content.Intent
import android.util.Log
import android.view.animation.Animation
import android.view.animation.DecelerateInterpolator
import com.example.mytestdemo.BaseViewBindActivity
import com.example.mytestdemo.R
import com.example.mytestdemo.databinding.Activity3dCameraMainBinding


class Rotate3DActivity : BaseViewBindActivity<Activity3dCameraMainBinding>() {

    private lateinit var rotate3d:ObjectAnimator
    private var isReversal = false

    private lateinit var customRotate3d:Rotation3dAnimation

    override fun getLayoutInflateId(): Int {
        return R.layout.activity_3d_camera_main
    }

    override fun initData() {
        //传统属性动画，利用属性参数
//        rotation1()

        //自定义属性动画，利用camera进行
        rotation2()
    }

    override fun initUI() {

        mBind.btClock.setOnClickListener{
            startActivity(Intent(this,RotateClockActivity::class.java))
        }
        mBind.btTestMatrix.setOnClickListener{
            startActivity(Intent(this,TestMatrixActivity::class.java))
        }
    }

    private fun rotation1() {
        rotate3d =  ObjectAnimator.ofFloat(mBind.ivRotate, "rotationY",0f,180f)

        var propertyName = "rotation"
        mBind.btRotation.setOnClickListener{
            Log.d(TAG, "btRotation initData: ${isReversal}")
            if (isReversal){
                rotate3d =  ObjectAnimator.ofFloat(mBind.ivRotate, propertyName,180f,0f)
            }else{
                rotate3d =  ObjectAnimator.ofFloat(mBind.ivRotate, propertyName,0f,180f)
            }
            rotate3d.addUpdateListener {
                Log.d(TAG, "initData: ${it.animatedValue}")
                isReversal = (it.animatedValue==180f)
            }

            rotate3d.duration=5000
            rotate3d.start()
        }
    }

    private fun rotation2() {
        customRotate3d = Rotation3dAnimation(0f,90f,false)
        //如果fillAfter为true，该动画执行的转换在完成时将持续存在。
        // 如果未设置，默认为false“false的话动画结束后会被还原”。 注意，这适用于单个动画，以及当使用AnimationSet来链接动画时。
        customRotate3d.fillAfter = true
        customRotate3d.duration = 5000

        customRotate3d.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation?) {
            }

            override fun onAnimationEnd(animation: Animation?) {
                mBind.ivRotate.setImageDrawable(resources.getDrawable(R.drawable.ic_no_data))
                customRotate3d = Rotation3dAnimation(90f,180f,true)
                customRotate3d.fillAfter= true
                customRotate3d.duration = 5000
                customRotate3d.interpolator = DecelerateInterpolator()
                mBind.llContent.startAnimation(customRotate3d)
            }

            override fun onAnimationRepeat(animation: Animation?) {
            }

        })
        mBind.btRotation.setOnClickListener{
//            Log.d(TAG, "btRotation initData: ${isReversal}")
//            if (isReversal){
//                rotate3d =  ObjectAnimator.ofFloat(mBind.ivRotate, propertyName,180f,0f)
//            }else{
//                rotate3d =  ObjectAnimator.ofFloat(mBind.ivRotate, propertyName,0f,180f)
//            }
//            rotate3d.addUpdateListener {
//                Log.d(TAG, "initData: ${it.animatedValue}")
//                isReversal = (it.animatedValue==180f)
//            }
            mBind.llContent.startAnimation(customRotate3d)

        }
    }
}